package com.mini.cn.goods.common.frame;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class NetWorkManager {

	private static volatile NetWorkManager manager;
	private static byte lock[]=new byte[0];
	public HashMap<RequestInfo, BaseNetWork>  netHashMap;
	private List<INetObserver> observers;
	private NetWorkManager(){
		netHashMap=new HashMap<RequestInfo, BaseNetWork>();
		observers=new ArrayList<INetObserver>();
	}

	public static NetWorkManager getInstance(){
		if(manager==null){
			synchronized (lock) {
				if(manager==null){
					manager=new NetWorkManager();
				}
			}
		}
		return manager;
	}

	public void registerObserver(INetObserver observer){
		observers.add(observer);
	}


	public void unRegisterObserver(INetObserver observer){
		observers.remove(observer);
	}

	public void sendObserver(int what,HashMap<String,Object> data,BaseNetAns ans){
		for(int i=0;i<observers.size();i++){
			INetObserver observer=observers.get(i);
			switch (what) {
			case NetWorkConstans.READY_STATE:
				observer.onReadyNetWork(ans);
				break;
			case NetWorkConstans.START_STATE:
				observer.onStartNetWork(ans);
				break;
			case NetWorkConstans.DOWNING_STATE:
				byte[] buf = (byte[])data.get("bytes");
				long downsize = (Long)data.get("downsize");
				int len=(Integer)data.get("len");
				observer.onDowningNetWork(buf ,len,downsize,ans);
				break;
			case NetWorkConstans.RECEIVE_STATE:
				observer.onReceiveNetWork(ans);
				break;
			case NetWorkConstans.STOP_STATE:
				observer.onStopNetWork(ans);
				break;
			case NetWorkConstans.ERROR_STATE:
				observer.onErrorNetWork(ans);
				break;
			default:
				break;
			}
		}

	}

}
